﻿using System;
using System.Text.RegularExpressions;

namespace Ops.Mpc.ValidationServices.Attributes
{
    [AttributeUsage(AttributeTargets.Property)]
    public class RequiredEmailAddressAttribute : ValidationAttribute
    {
        private static readonly string patternStrict = @"^(([^<>()[\]\\.,;:\s@\""]+"
      + @"(\.[^<>()[\]\\.,;:\s@\""]+)*)|(\"".+\""))@"
      + @"((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}"
      + @"\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+"
      + @"[a-zA-Z]{2,}))$";

        #region IValidationAttribute Members

        public override bool Validate(object container, object data, string[] propertyNames)
        {
            string emailAddress = data as string;

            return !string.IsNullOrEmpty(emailAddress) && new Regex(patternStrict,RegexOptions.IgnoreCase).IsMatch(emailAddress);
        }

        public override string FormatMessage(string messageFormat, object container, object data, string name)
        {
            return string.Format(messageFormat, name);
        }
        #endregion
    }
}
